#include "UniformBuffer.h"
#include <SERender/Resourses/Shader.h>
#include <SEDebug/Log/Log.h>
#include <glad/glad.h>
#include <SERender/Util/Util.h>

namespace SEngine
{
    UniformBuffer::UniformBuffer(std::unique_ptr<IDrive> && drive, size_t totalSize, const std::string &name, uint32_t bindPoint)
        : m_name(name)
        , m_bindPoint(bindPoint)
        , m_drive(std::move(drive))
    { 
        glCall(glGenBuffers(1, &m_id)); 
        Bind();
        glCall(glBufferData(GL_UNIFORM_BUFFER, totalSize, nullptr, GL_DYNAMIC_DRAW));
        glCall(glBindBufferBase(GL_UNIFORM_BUFFER, bindPoint, m_id)); 
        Unbind();
    }

    UniformBuffer::~UniformBuffer()
    { 
        glDeleteBuffers(1, &m_id); 
    }

    void UniformBuffer::Bind()
    { 
        glCall(glBindBuffer(GL_UNIFORM_BUFFER, m_id)); 
    }

    void UniformBuffer::Unbind()
    { 
        glCall(glBindBuffer(GL_UNIFORM_BUFFER, 0)); 
    }

    void UniformBuffer::BindToShader(Shader& shader, const std::string &name, uint32_t bindPoint)
    {
        auto loaction = glGetUniformBlockIndex(shader.GetId(), name.c_str());
        glUniformBlockBinding(shader.GetId(), loaction, bindPoint);
    }



} // namespace SEngine
